/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.aionemu.gameserver.model.gameobjects;

import javolution.util.FastMap;

import com.aionemu.gameserver.model.drop.DropItem;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.state.CreatureState;
import com.aionemu.gameserver.model.gameobjects.state.CreatureVisualState;
import com.aionemu.gameserver.model.gameobjects.stats.CreatureGameStats;
import com.aionemu.gameserver.model.templates.NpcWhichNotGatherableTemplate;
import com.aionemu.gameserver.model.templates.spawn.SpawnTemplate;
import com.aionemu.gameserver.services.DropService;
import com.aionemu.gameserver.taskmanager.tasks.RespawnTaskManager;

/**
 * @author lyahim
 *
 */
public abstract class NpcWhichNotGatherable extends VisibleObject
{

	private int					state		= CreatureState.ACTIVE.getId();
	private int					visualState	= CreatureVisualState.VISIBLE.getId();
	private CreatureGameStats<? extends NpcWhichNotGatherable>	gameStats;
	private FastMap<Integer, DropItem>	currentDropMap;
	
	public NpcWhichNotGatherable(int objId, SpawnTemplate spawnTemplate)
	{
		super(objId, spawnTemplate);
	}

	@Override
	public NpcWhichNotGatherableTemplate getObjectTemplate()
	{
		return (NpcWhichNotGatherableTemplate) objectTemplate;
	}

	/**
	 * @return Returns the gameStats.
	 */
	public CreatureGameStats<? extends NpcWhichNotGatherable> getGameStats()
	{
		return gameStats;
	}

	/**
	 * @param gameStats The gameStats to set.
	 */
	public void setGameStats(CreatureGameStats<? extends NpcWhichNotGatherable> gameStats)
	{
		this.gameStats = gameStats;
	}

	/**
	 * Schedule respawn of npc
	 * In instances - no npc respawn
	 */
	public void scheduleRespawn()
	{
		if (getSpawn() != null)
		{
			if (isInInstance())
				return;

			if (getSpawn().getInterval() > 0)
			{
				RespawnTaskManager.getInstance().addRespawnTask(this);
			}
		}
	}

	public int getState()
	{
		return state;
	}

	public void setState(CreatureState state)
	{
		this.state |= state.getId();
	}

	public void setState(int state)
	{
		this.state = state;
	}

	public void unsetState(CreatureState state)
	{
		this.state &= ~state.getId();
	}

	public boolean isInState(CreatureState state)
	{
		int isState = this.state & state.getId();

		if (isState == state.getId())
			return true;

		return false;
	}

	public int getVisualState()
	{
		return visualState;
	}

	public void setVisualState(CreatureVisualState visualState)
	{
		this.visualState |= visualState.getId();
	}

	public void unsetVisualState(CreatureVisualState visualState)
	{
		this.visualState &= ~visualState.getId();
	}

	public boolean isInVisualState(CreatureVisualState visualState)
	{
		int isVisualState = this.visualState & visualState.getId();

		if (isVisualState == visualState.getId())
			return true;

		return false;
	}

	public byte getLevel()
	{
		return getObjectTemplate().getLevel();
	}

	public abstract void onDie(Creature lastAttacker);
	
	public Relations getRelation(Player player)
	{
		return Relations.NOT_ATTACKABLE;
	}

	/**
	 * @return Returns the currentDropMap.
	 */
	public FastMap<Integer, DropItem> getCurrentDropMap()
	{
		return currentDropMap;
	}

	/**
	 * @param currentDropMap The currentDropMap to set.
	 */
	public void setCurrentDropMap(FastMap<Integer, DropItem> currentDropMap)
	{
		if (currentDropMap == null && this.currentDropMap != null)
			FastMap.recycle(this.currentDropMap);
		this.currentDropMap = currentDropMap;
	}
	
	@Override
	public void onDespawn()
	{
		super.onDespawn();
		unsetState(CreatureState.DEAD);
		DropService.getInstance().unregisterDrop(this);
	}
	
	public short getNpcTypeId()
	{
		return 1;
	}
}
